﻿using System;
using System.Linq;
using System.Windows.Data;
using MVVM_S.Samples.HelpDesk.ViewModels;
using MVVM_S.Samples.HelpDesk.Web.Models;

namespace MVVM_S.Samples.HelpDesk.Converters
{
    public class UserConverter : IValueConverter
    {
        PagedCollectionView lookupCollection;

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (lookupCollection == null)
            {
                LookupsViewModel lookupModel = (LookupsViewModel)App.Current.Resources["LookupsVM"];
                lookupCollection = lookupModel.UserList;
            }
            if (value is int)
            {
                User selUser = lookupCollection.Cast<User>()
                    .FirstOrDefault(u => u.UserID == (int)value);
                if (selUser != null)
                    return selUser.DisplayName;
            }
            if (value is string)
            {
                User selUser = lookupCollection.Cast<User>()
                    .FirstOrDefault(u => u.UserName == (string)value);
                if (selUser != null)
                    return selUser.DisplayName;
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
